package com.zone.lib.download.utils;

import android.content.Context;
import android.util.Log;

import com.zone.lib.download.DownloadHelper;
import com.zone.lib.download.db.DownloadDaoUtilsStore;
import com.zone.lib.download.db.entity.Download;
import com.zone.lib.download.enums.AppStatusEnum;
import com.zone.sdk.module.appstore.base.StoreUtils;
import com.zone.sdk.module.appstore.entity.AppDetailEntity;
import com.zone.sdk.module.appstore.entity.ResourceItem;

import org.apache.commons.lang3.ObjectUtils;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import static com.zone.sdk_api.common.Content.VIN;

/**
 * @Author: pkqlqx
 * @CreateDate: 2021/12/30 19:41
 * @Description:
 */
public class DownloadUtil {

    private static final String TAG = DownloadUtil.class.getSimpleName();

    public static void downLoad(Context context, int status, String userId, AppDetailEntity appEntity){
        Log.d(TAG, "downLoad status="+status+" code="+appEntity.getCode());
        if (ObjectUtils.isNotEmpty(appEntity)){
            File dir = new File(context.getCacheDir(), "download");
            if (!dir.exists()){
                dir.mkdirs();
            }
            File saveFile = new File(dir, appEntity.getCode()+".apk");
            Download download = DownloadDaoUtilsStore.getInstance().getDownloadByCode(appEntity.getCode());
            // TODO for test
//            if (ObjectUtils.isNotEmpty(download) && download.getDownloadStatus() == AppStatusEnum.DOWNLOAD_COMPLETE.CODE){
//                DownloadDaoUtilsStore.getInstance().deleteByCode(download.getCode());
//                download = null;
//            }
            if (ObjectUtils.isEmpty(download)){
                download = build(status, appEntity, saveFile);
                DownloadDaoUtilsStore.getInstance().getDownloadAppDaoUtils().insert(download);
                DownloadHelper.getInstance().addTask(userId, saveFile, SharedPreferencesUtil.getInstance(context).getSP(VIN), status, appEntity, download).submit(context);
            } else {
                //暂停 或者 继续
                if (status == AppStatusEnum.DOWNLOAD_PAUSE.CODE
                        || status == AppStatusEnum.DOWNLOAD_PREPARE.CODE
                        || status == AppStatusEnum.DOWNLOAD_DELETE.CODE
                ){
                    DownloadHelper.getInstance().addTask(userId, saveFile, SharedPreferencesUtil.getInstance(context).getSP(VIN), status, appEntity, download).submit(context);
                }
            }
        }
    }

    public static Download build(int status, AppDetailEntity appEntity, File saveFile) {
        if (ObjectUtils.isEmpty(appEntity)){
            return null;
        }
        Download download = new Download();
        download.setCode(appEntity.getCode());
        download.setPackageName(appEntity.getPackageName());
        download.setDownloadUrl(appEntity.getUrl());
        download.setName(appEntity.getName());
        download.setDownloadStatus(status);
        if (saveFile != null) {
            download.setFilePath(saveFile.getPath());
        }
        download.setDownloadLocation(0);
        download.setAppType(appEntity.getStatusDictionaryType());
        download.setVersion(appEntity.getVersion());
        download.setOneWord(appEntity.getDescription());
        download.setCreateTime(DateUtils.getStringDate());
        download.setIcon(appEntity.getIconUrl());
        ResourceItem item = appEntity.getPreview();
        download.setPreview(item.getResourceUrl());
        download.setPreviewType(item.getResourceType());
        download.setIsFree(!appEntity.needPay());
        return download;
    }

    public static AppDetailEntity build(Download download){
        if (ObjectUtils.isEmpty(download)){
            return null;
        }
        AppDetailEntity appEntity = new AppDetailEntity();
        appEntity.setCode(download.getCode());
        appEntity.setName(download.getName());
        appEntity.setPackageName(download.getPackageName());
        appEntity.setUrl(download.getDownloadUrl());
        appEntity.setAppType(download.getAppType());
        appEntity.setVersion(download.getVersion());
        appEntity.setOneWord(download.getOneWord());
        appEntity.setIconUrl(download.getIcon());
        ResourceItem item = new ResourceItem();
        item.setResourceType(download.getPreviewType());
        item.setResourceUrl(download.getPreview());
        item.setResourceId(StoreUtils.RESOURCE_ID_PREVIEW);
        List<ResourceItem> resourceItemList = new LinkedList<>();
        resourceItemList.add(item);
        appEntity.setResourceList(resourceItemList);
        return appEntity;
    }

}
